package com.tao.power.config;

import com.tao.power.Dao.MenusMapper;
import com.tao.power.Pojo.Menu;
import com.tao.power.Pojo.User;
import com.tao.power.Service.LoginService;
import com.tao.power.Service.MenuService;
import com.tao.power.Utils.tokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class LoadInterceptor implements HandlerInterceptor {

    @Autowired
    private LoginService loginService;
    @Autowired
    private MenusMapper menusMapper;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String Authorization = request.getHeader("Authorization");
        return tokenUtils.JwtVerify(Authorization);
//        String pathInfo = request.getServletPath();
//        String[] split = pathInfo.split("/");
//        //获取请求路径
//        String address = split[2];
//        HttpSession session = request.getSession();
//        User user = (User) session.getAttribute("user");
//        //查询此路径是不是功能点，不是放行.
//        String power = menusMapper.queryMenuByAddress(address);
//        if (power == null || !power.equals("2")) {
//            return token;
//        } else {
//            //是,看有没有权限，没有拦截，有放行。
//            List<String> list= loginService.queryPowerByUser(user.getId());
//            if (!list.contains(address)) {
//                response.sendError(4000, "你暂无此功能权限!");
//                return false;
//            }
//        }
//        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //System.out.println("执行了TestInterceptor的postHandle方法");
    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

}
